﻿using System;

namespace CustomerContactSystem.WebForms.Admin.Reps
{
    public partial class Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsValidPageRequest(true, true, "id")) {
                return;
            }

            GetRep();
        }

        private void GetRep() {
            var repId = int.Parse(Request.QueryString["id"]);
            var rep = RepRepository.GetById(repId);

            if (rep == null) {
                return;
            }

            FirstName.Text = rep.FirstName;
            SecondName.Text = rep.SecondName;
            Email.Text = rep.Email;
            IsActive.Checked = rep.IsActive;
        }

        protected void UpdateRepClick(object sender, EventArgs e)
        {
            if (!IsValidPageRequest(true, false, "id")) {
                return;
            }
            
            var repId = int.Parse(Request.QueryString["id"]);
            var rep = RepRepository.GetById(repId);

            if (rep == null) {
                return;
            }

            rep.FirstName = FirstName.Text;
            rep.SecondName = SecondName.Text;
            rep.Email = Email.Text;
            rep.IsActive = IsActive.Checked;

            if (!IsValidObject(rep, ErrorMsg)) {
                return;
            }

            RepRepository.Update(rep);
            Response.Redirect("~/Admin/Reps/List.aspx#" + rep.Id);
        }
    }
}